/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ogawa.base.utils.imageloader.glide;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.ogawa.base.utils.GlideRequest;
import com.ogawa.base.utils.GlideRequests;
import com.ogawa.base.utils.imageloader.BaseImageLoaderStrategy;
import com.ogawa.base.utils.imageloader.ImageConfigImpl;




/**
 * ================================================
 * 此类只是简单的实现了 Glide 加载的策略,方便快速使用,但大部分情况会需要应对复杂的场景
 * ================================================
 */
public class GlideImageLoaderStrategy implements BaseImageLoaderStrategy<ImageConfigImpl> {


    @Override
    public void loadImage(Context ctx, ImageConfigImpl config) {
        try{

            GlideRequests requests;

            requests = (GlideRequests) Glide.with(ctx);//如果context是activity则自动使用Activity的生命周期

            GlideRequest<Drawable> glideRequest = requests.load(config.getUrl());

            switch (config.getCacheStrategy()) {//缓存策略
                case 0:
                    glideRequest.diskCacheStrategy(DiskCacheStrategy.ALL);
                    break;
                case 1:
                    glideRequest.diskCacheStrategy(DiskCacheStrategy.NONE);
                    break;
                case 2:
                    glideRequest.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
                    break;
                case 3:
                    glideRequest.diskCacheStrategy(DiskCacheStrategy.DATA);
                    break;
                case 4:
                    glideRequest.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
                    break;
                default:
                    glideRequest.diskCacheStrategy(DiskCacheStrategy.ALL);
                    break;
            }

            if (config.isCrossFade()) {
                glideRequest.transition(DrawableTransitionOptions.withCrossFade());
            }

            if (config.isCenterCrop()) {
                glideRequest.centerCrop();
            }

            if (config.isCircle()) {
                glideRequest.circleCrop();
            }

            if (config.isImageRadius()) {
                glideRequest.transform(new RoundedCorners(config.getImageRadius()));
            }

            if (config.isBlurImage()) {
                glideRequest.transform(new BlurTransformation(config.getBlurValue()));
            }

            if (config.getTransformation() != null) {//glide用它来改变图形的形状
                glideRequest.transform(config.getTransformation());
            }

            if (config.getPlaceholder() != 0)//设置占位符
                glideRequest.placeholder(config.getPlaceholder());

            if (config.getErrorPic() != 0)//设置错误的图片
                glideRequest.error(config.getErrorPic());

            if (config.getFallback() != 0)//设置请求 url 为空图片
                glideRequest.fallback(config.getFallback());
            glideRequest
                    .into(config.getImageView());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void clear(final Context ctx, ImageConfigImpl config) {
        try{
            if (config.getImageView() != null) {
                Glide.get(ctx).getRequestManagerRetriever().get(ctx).clear(config.getImageView());
            }

            if (config.getImageViews() != null && config.getImageViews().length > 0) {//取消在执行的任务并且释放资源
                for (ImageView imageView : config.getImageViews()) {
                    Glide.get(ctx).getRequestManagerRetriever().get(ctx).clear(imageView);
                }
            }

            if (config.isClearDiskCache()) {//清除本地缓存
                Glide.get(ctx).clearDiskCache();
//                Completable.fromAction(new Action() {
//                    @Override
//                    public void run() throws Exception {
//                        Glide.get(ctx).clearDiskCache();
//                    }
//                }).subscribeOn(Schedulers.io()).subscribe();
            }

            if (config.isClearMemory()) {//清除内存缓存
                Glide.get(ctx).clearMemory();
//                Completable.fromAction(new Action() {
//                    @Override
//                    public void run() throws Exception {
//                        Glide.get(ctx).clearMemory();
//                    }
//                }).subscribeOn(AndroidSchedulers.mainThread()).subscribe();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
